(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:AccountvalueCtrl
     * @description
     * # AccountvalueCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('AccountvalueCtrl', AccountvalueCtrl);

    AccountvalueCtrl.$inject = ['$scope', 'dataService', '$location', 'cacheService', '$rootScope'];

    function AccountvalueCtrl($scope, dataService, $location, cacheService, $rootScope) {
        $scope.listAction = function() {
            $scope.asc = 1;
            $scope.page_Size = 10;
            $scope.getlist(1);
        }

        $scope.getlist = function(pageNo, member_id, type) {
            var query = {};
            var key = $rootScope.store_id + "accountValue";
            var cache = cacheService.get(key);
            if (pageNo || type || !cache) {
                $scope.pageNo = pageNo ? pageNo : "1";
                var query = {
                    page_no: $scope.pageNo,
                    page_size: $scope.page_Size
                };

                if (member_id) {
                    query.query = {
                        "member_id": member_id
                    };
                }
                if (type) {
                    $scope.type = type;
                    $scope.asc = $scope.asc == 1 ? 0 : 1;
                }

                if ($scope.type) {
                    query.order_by = $scope.asc ? $scope.type + ":desc" : $scope.type + ":asc";
                } else {
                    query.order_by = "integral:desc";
                }
                cacheService.set(key, query);
            } else {
                query = clone(cache);
                // $scope.queryRcache(query);
            }

            var querys = clone(query);
            dataService.accountvaleGet(querys).then(function(list) {
                if (list.accounts) {
                    if ($scope.account_id) {
                        $scope.account = list.accounts[0];
                    } else {
                        $scope.accounts = list.accounts;
                        $scope.total_count = list.total_count;
                    }
                }
            });
        }

        //选择用户回调方法
        $scope.memberdata = function(memberInfo) {
            $scope.member_id = memberInfo.member_id;
        }

        $scope.accountAction = function(pageNo) {
            $scope.member_id = $location.search().member_id;
            var query = {
                page_no: pageNo ? pageNo : "1",
                page_size: $scope.page_Size,
                query: {
                    member_id: $scope.member_id
                }
            };
            dataService.memberAccountGet(query).then(function(list) {
                if (list.accounts) {
                    $scope.member = list.accounts;
                    $scope.total_count = list.total_count;
                }
            });
        }

        $scope.infoAction = function(pageNo) {
            $scope.member_id = $location.search().member_id;
            $scope.getlist(1, $scope.member_id);
        }

    }
})();